package figure

type GridSetting struct {
	cells      [9][9]uint8
	candidates [9][9][9]bool
}

func (gs *GridSetting) Set(rowNum int, colNum int, value uint8) {
	if value > 9 {
		return
	}
	gs.cells[rowNum][colNum] = value
}

func (gs *GridSetting) Input(in string) {
	buff := [81]uint8{}
	len := 0
	for _, c := range []byte(in) {
		if c >= '0' && c <= '9' {
			buff[len] = c - '0'
			len++
		}
		if c == '.' {
			len++
		}
		if len == 81 {
			break
		}
	}
	len = 0
	for i := 0; i < 9; i++ {
		for j := 0; j < 9; j++ {
			gs.cells[i][j] = buff[len]
			len++
		}
	}
}

func (gs *GridSetting) Stringer() string {
	b := make([]byte, 81)
	n := 0
	for i := 0; i < 9; i++ {
		for j := 0; j < 9; j++ {
			b[n] = uint8(gs.cells[i][j]) + '0'
			n++
		}
	}
	return string(b)
}

func (gs *GridSetting) SetCandidates(rowNum int, colNum int, candidates string) {
	for _, c := range []byte(candidates) {
		if c < '1' || c > '9' {
			continue
		}
		p := c - '0' - 1
		gs.candidates[rowNum][colNum][p] = true
		gs.cells[rowNum][colNum] = 0
	}
}
